package org.jeecg.modules.xxgx.smsq.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.config.sign.util.SignUtil;
import org.jeecg.modules.xxgx.common.service.IXxBase;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdj;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjmx;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjService;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjmxService;
import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxx;
import org.jeecg.modules.xxgx.fpgl.service.ICsXxFpxxService;
import org.jeecg.modules.xxgx.gdmp.entity.CsXxGdmp;
import org.jeecg.modules.xxgx.gdmp.service.ICsXxGdmpService;
import org.jeecg.modules.xxgx.smsq.dto.CsYkSmsqDTO;
import org.jeecg.modules.xxgx.smsq.entity.CsYkSmsq;
import org.jeecg.modules.xxgx.smsq.service.ICsYkSmsqService;
import org.jeecg.modules.xxgx.smsq.vo.CsXxDkdjVO;
import org.jeecg.modules.xxgx.smsq.vo.CsXxGdmpVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 扫码申请
 * @Author: jeecg-boot
 * @Date:   2023-08-14
 * @Version: V1.0
 */
@Api(tags="扫码申请")
@RestController
@RequestMapping("/smsq/csYkSmsq")
@Slf4j
public class CsYkSmsqController extends JeecgController<CsYkSmsq, ICsYkSmsqService> {
	@Autowired
	private ICsYkSmsqService csYkSmsqService;
	 @Autowired
	 private ICsXxGdmpService csXxGdmpService;

	 @Autowired
	 private ICsXxDkdjService csXxDkdjService;
	 @Autowired
	 private ICsXxDkdjmxService csXxDkdjmxService;

	 @Autowired
	 private ICsXxFpxxService csXxFpxxService;

	 @Autowired
	 IXxBase xxBase;
	
	/**
	 * 分页列表查询
	 *
	 * @param csYkSmsq
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "扫码申请-分页列表查询")
	@ApiOperation(value="扫码申请-分页列表查询", notes="扫码申请-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CsYkSmsq>> queryPageList(CsYkSmsq csYkSmsq,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CsYkSmsq> queryWrapper = QueryGenerator.initQueryWrapper(csYkSmsq, req.getParameterMap());
		Page<CsYkSmsq> page = new Page<CsYkSmsq>(pageNo, pageSize);
		IPage<CsYkSmsq> pageList = csYkSmsqService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param csYkSmsqDTO
	 * @return
	 */
	@AutoLog(value = "扫码申请-添加")
	@ApiOperation(value="扫码申请-添加", notes="扫码申请-添加")
	@PostMapping(value = "/add")
	public Result add(@RequestBody CsYkSmsqDTO csYkSmsqDTO) {
		return csYkSmsqService.save(csYkSmsqDTO);
	}
	
	/**
	 *  编辑
	 *
	 * @param csYkSmsq
	 * @return
	 */
	@AutoLog(value = "扫码申请-编辑")
	@ApiOperation(value="扫码申请-编辑", notes="扫码申请-编辑")
	@RequiresPermissions("smsq:cs_yk_smsq:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CsYkSmsq csYkSmsq) {
		csYkSmsqService.updateById(csYkSmsq);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "扫码申请-通过id删除")
	@ApiOperation(value="扫码申请-通过id删除", notes="扫码申请-通过id删除")
	@RequiresPermissions("smsq:cs_yk_smsq:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		csYkSmsqService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "扫码申请-批量删除")
	@ApiOperation(value="扫码申请-批量删除", notes="扫码申请-批量删除")
	@RequiresPermissions("smsq:cs_yk_smsq:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.csYkSmsqService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "扫码申请-通过id查询")
	@ApiOperation(value="扫码申请-通过id查询", notes="扫码申请-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CsYkSmsq> queryById(@RequestParam(name="id",required=true) String id) {
		CsYkSmsq csYkSmsq = csYkSmsqService.getById(id);
		if(csYkSmsq==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(csYkSmsq);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param csYkSmsq
    */
    @RequiresPermissions("smsq:cs_yk_smsq:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CsYkSmsq csYkSmsq) {
        return super.exportXls(request, csYkSmsq, CsYkSmsq.class, "扫码申请");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("smsq:cs_yk_smsq:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CsYkSmsq.class);
    }

	 @ApiOperation(value="固定码牌-通过id查询", notes="固定码牌-通过id查询")
	 @GetMapping(value = "/queryGdmpById")
	 public Result<CsXxGdmpVO> queryGdmpById(@RequestParam(name="id",required=true) String id) {
		 CsXxGdmp csXxGdmp = csXxGdmpService.getById(id);
		 if(csXxGdmp==null) {
			 return Result.error("未找到对应数据");
		 }
		 CsXxGdmpVO csXxGdmpVO = new CsXxGdmpVO();
		 BeanUtils.copyProperties(csXxGdmp,csXxGdmpVO);

		 csXxGdmpVO.setSign(SignUtil.getParamsSign(csXxGdmp.getId()));
		 return Result.OK(csXxGdmpVO);
	 }
	 @GetMapping(value = "/comSearch")
	 public Result<JSONArray> comSearchApi(@RequestParam(name="searchName", required = true) String searchName,
											@RequestParam(name="signId", required = true) String signId,
											@RequestParam(name="sign", required = true) String sign,
												  HttpServletRequest req) {
		 if(!SignUtil.verifySign(signId,sign)){
			 return Result.error("无权限");
		 }
		 Result result=null;
		 result = xxBase.comSearch(searchName);
		 if(result.isSuccess()){
			 JSONObject dataJson = new JSONObject();
			 dataJson.put("groups",result.getResult());
			 result.setResult(dataJson);
		 }
		 return result;
	 }

	 @ApiOperation(value="扫码申请-通过待开单据ID查询待开单据明细", notes="扫码申请-通过待开单据ID查询待开单据明细")
	 @GetMapping(value = "/queryDkdjById")
	 public Result<CsXxDkdjVO> queryDkdjById(@RequestParam(name="ddid",required=true) String ddid) {
		 CsXxDkdj csXxDkdj = csXxDkdjService.getById(ddid);
		 if(csXxDkdj==null) {
			 //查询已开单据判断是否已开票，如已开票，则返回开票信息
			 List<CsXxFpxx> list = csXxFpxxService.lambdaQuery().eq(CsXxFpxx::getDjbh, ddid).list();
			 if(list != null && list.size() > 0){
				 CsXxFpxx csXxFpxx = list.get(0);

				 Result result = new Result();
				 result.setSuccess(true);
				 result.setCode(2001);
				 result.setResult(csXxFpxx.getId());
				 return result;
			 } else {
				 return Result.error("未找到对应数据");
			 }
		 }

		 CsXxDkdjVO csXxDkdjVO = new CsXxDkdjVO();
		 BeanUtils.copyProperties(csXxDkdj, csXxDkdjVO);

		 csXxDkdjVO.setSign(SignUtil.getParamsSign(csXxDkdj.getId()));

		 //查询明细
		 List<CsXxDkdjmx>  dkdjmx = csXxDkdjmxService.selectByMainId(ddid);
		 csXxDkdjVO.setDkdjmx(dkdjmx);


		 return Result.OK(csXxDkdjVO);
	 }
}
